package com.kangyi.user.controller;

import com.kangyi.dto.AddNursingHomeBooking;
import com.kangyi.result.Result;
import com.kangyi.user.service.NursingHomeBookingService;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.NursingHomeBookingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/user/book")
@Slf4j
@Api(tags = "预约养老院相关功能")
public class NursingHomeBookingController {

    @Autowired
    private NursingHomeBookingService nursingHomeBookingService;

    @GetMapping("/inquiry/{userId}")
    @ApiOperation("查询当前用户下申请的预约")
    @Cacheable(cacheNames = "NursingHomeBooking_",key = "'userInquiry_'+#userId")
    public Result<List<NursingHomeBookingVO>> inquiryNursingHomeBooking(@PathVariable Integer userId){
        log.info("查询当前用户下申请的预约：{}",userId);
        try {
            AuthUtil.validateUserPermission(userId);
            List<NursingHomeBookingVO> nursingHomeBookingVOS = nursingHomeBookingService.inquiryNursingHomeBooking(userId);
            return Result.success(nursingHomeBookingVOS,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除预约养老院记录")
    @CacheEvict(cacheNames = "NursingHomeBooking_",allEntries = true)
    public Result deleteNursingHomeBooking(@PathVariable Integer id){
        log.info("删除预约养老院记录：{}",id);
        try {
            nursingHomeBookingService.deleteNursingHomeBooking(id);
            return Result.success("删除成功!");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("预约养老院")
    @CacheEvict(cacheNames = "NursingHomeBooking_",allEntries = true)
    public Result addNursingHomeBooking(@RequestBody AddNursingHomeBooking addNursingHomeBooking){
        log.info("预约养老院：{}",addNursingHomeBooking);
        try {
            AuthUtil.validateUserPermission(addNursingHomeBooking.getUserId());
            nursingHomeBookingService.addNursingHomeBooking(addNursingHomeBooking);
            return Result.success("预约成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

 
}
